//
//  PopoverPresentationController.swift
//  Weibo
//
//  Created by 杨雨哲 on 15/6/29.
//  Copyright © 2015年 yyz. All rights reserved.
//

import UIKit

class PopoverPresentationController: UIPresentationController {
    
    var presentFrame = CGRectZero
    
    lazy var dummyView:UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(white: 0.0, alpha: 0.2)
        return v
    }()
    
    override init(presentedViewController: UIViewController, presentingViewController: UIViewController) {
        
        super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController)
        
        prepareDummyView()
    }
    
    func close(){
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }
    
    private func prepareDummyView(){
        
        let tap = UITapGestureRecognizer(target: self, action: "close")
        dummyView.addGestureRecognizer(tap)
        
    }
    
    override func containerViewWillLayoutSubviews() {
        
        super.containerViewWillLayoutSubviews()
        
        dummyView.frame = containerView!.bounds
        
        containerView?.insertSubview(dummyView, atIndex: 0)
        
        presentedView()?.frame = presentFrame
        
    }

}
